package com.leetcode.algorithm.y22.m08.w1;

/**
 * 162. 寻找峰值
 * 
 * https://leetcode.cn/problems/find-peak-element/
 * 
 * @author jie.deng
 *
 */
class Question0162Solution01 {

	public int findPeakElement(int[] nums) {
		int len = nums.length;
		if (len == 1) {
			return 0;
		}
		if (nums[0] > nums[1]) {
			return 0;
		}
		if (nums[len - 1] > nums[len - 2]) {
			return len - 1;
		}
		return findPeakElement(nums, 0, len - 1);
	}

	private int findPeakElement(int[] nums, int left, int right) {
		if (right - left <= 1) {
			return -1;
		}
		int mid = left + ((right - left) >> 1);
		if (nums[mid] > nums[mid - 1] && nums[mid] > nums[mid + 1]) {
			return mid;
		}
		int idx = findPeakElement(nums, left, mid);
		if (idx == -1) {
			idx = findPeakElement(nums, mid, right);
		}
		return idx;
	}

}